package com.sc3.game.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.sc3.game.service.UserService;
import com.sc3.game.service.UserServiceAsync;

public class LoginPage extends Composite {

	private static LoginPageUiBinder uiBinder = GWT
			.create(LoginPageUiBinder.class);
	
	private static final UserServiceAsync USER_SERVICE = GWT
	.create(UserService.class);


	interface LoginPageUiBinder extends UiBinder<Widget, LoginPage> {
	}

	public LoginPage() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiField
	Button commit;
	@UiField
	TextBox email;
	@UiField
	PasswordTextBox password;
	
	@UiHandler("commit")
	public void onClickCommit(ClickEvent event){
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(Boolean result) {
				if(result){
					Window.alert("login success");
				} else{
					Window.alert("login failed!");
				}
			}
		};
		USER_SERVICE.login(email.getText(), password.getText(), callback);
		
	}

}
